Explainable AI¶
Nutshell¶
In this project I use DataRobot to predict the type of food from images, as explained on the course Modern Artificial Intelligence, lectured by Dr. Ryan Ahmed, Ph.D. MBA. DataRobot is an end-to-end enterprise AI platform that automates and accelerates every step from data to value.
Data¶
The original dataset from https://www.kaggle.com/vermaavi/food11 consists of 16643 color images belonging to 11 categories. Due to data limitations I will use pictures from 4 classes only:
- Dessert
- Seafood
- Fried food
- Vegetable-Fruit
Grad-CAM visualization¶
Gradient-Weighted Class Activation Mapping (Grad-CAM) makes it possible to visualize the regions of the input that contributed towards making prediction by the model. It does so by using the class- specific gradient information flowing into the final convolutional layer of CNN to localize the important regions in the image that resulted in predicting that particular class.
Steps¶
- To visualize the activation maps, first the image has to be passed through the model to make the prediction. Using argmax find the index corresponding to the maximum value in the prediction - this is the predicted class.
- Next, the gradient that is used to arrive to the predicted class from the feature map activations A^k is calculated.
$$ \alpha = \frac{1}{Z} \sum_i \sum_j \frac{\partial y^c}{\partial A^k_{ij}}$$ where $$\frac{1}{Z} \sum_i = \text{global average pooling}$$ $$\frac{\partial y^c}{\partial A^k_{ij}} = \text{gradients via backdrop}$$
- To enhance the filter values that resulted in this prediction, the values are multiplied with tensorflow.GradientTape() with the filter values in the last convolutional layer.
- This enhances the filter values that contributed towards making this prediction and lower the filter values that didn't contribute.
- Then, the weighted combination of activation maps is performed and followed by ReLU to obtain the heatmap.
$$ L^c_{Grad-CAM}= ReLU(\sum \alpha^c_kA^k) $$ where
Finally, the feature heatmap is super-imposed on the original image to see the activation locations in the image.
The best performing model for this task was Regularized Logistic Regression (L2).
| Metric | Validation | Cross-validation | Holdout |
|---|---|---|---|
| AUC | 0.9788 | 0.9877 | 0.9822 |
| Accuracy | 0.8900 | 0.9188 | 0.9036 |
| Balanced Accuracy | 0.8885 | 0.9196 | 0.9045 |
| FVE Multinomial | 0.7699 | 0.8202 | 0.7815 |
| LogLoss | 0.3188 | 0.2475 | 0.3019 |
Below is an example from the DataRobot models attention maps.
Applying a Grad-CAM for the Brain tumor detector classifier model¶
Next I will implement a Grad-CAM pipeline for the model built in my other project. This model takes MRI images of the brain as input and classifies them into two classes: contains a brain tumor or not. You can check the project here: Brain tumor detector
Introduction to the Brain Tumor Detection¶
Deep learning has proven to be as good and even better than humans in detecting diseases from X-rays, MRI scans and CT scans. there is huge potential in using AI to speed up and improve the accuracy of diagnosis. This project will use the labeled dataset from https://www.kaggle.com/datasets/mateuszbuda/lgg-mri-segmentation which consists of 3929 Brain MRI scans and the tumor location. The final pipeline has a two step process where
- A Resnet deep learning classifier model will classify the input images into two groups: tumor detected and tumor not detected.
- For the images, where tumor was detected, a second step is performed, where a ResUNet segmentation model detects the tumor location on the pixel level.
Below is an exmaple of an MRI image and the matching mask. This example has a small tumor. In images where no tumor is present, the mask will be complety black.
Convolutional neural networks (CNNs)¶
- The first CNN layers are used to extract high level general features
- The last couple of layers will perform classification
- Locla respective fields scan the image first searching for simple shapes such as edges and lines
- The edges are picked up by the subsequent layer to form more complex features
A good visualisation of the feature extraction with convolutions can be found at https://setosa.io/ev/image-kernels/
ResNet (Residual Network)¶
- As CNNs grow deeper, vanishing gradients negatively imapct the network performance. Vanishing gradient occurs when the gradient is backpropagated to earlier layers which results in a very small gradient.
- ResNets "skip connection" feature can allow training of 152 layers wihtout vanishing gradient problems
- ResNet adds "identity mapping on top of the CNN
- ResNet deep network is trained with ImageNet, which contains 11 million images and 11 000 categories
ResNet paper (He etal, 2015): https://arxiv.org/pdf/1512.03385
As seen in the Figure 6. from the Resnet paper, the ResNet architectures overcome the training challenges from deep networks compared ot the plain networks. ResNet-152 achieved 3.58% error rate on the ImageNet dataset. This is better than human performance.
Found 2839 validated image filenames belonging to 2 classes. Found 500 validated image filenames belonging to 2 classes. Found 590 validated image filenames belonging to 2 classes.
/usr/local/lib/python3.12/dist-packages/keras/src/trainers/data_adapters/py_dataset_adapter.py:121: UserWarning: Your `PyDataset` class should call `super().__init__(**kwargs)` in its constructor. `**kwargs` can include `workers`, `use_multiprocessing`, `max_queue_size`. Do not pass these arguments to `fit()`, as they will be ignored.
37/37 ━━━━━━━━━━━━━━━━━━━━ 193s 5s/step
Assessment of the model¶
The model accuracy is 0.94
Classification report
precision recall f1-score support
0 0.93 0.98 0.95 366
1 0.97 0.88 0.92 224
micro avg 0.94 0.94 0.94 590
macro avg 0.95 0.93 0.94 590
weighted avg 0.94 0.94 0.94 590
Grad-CAM for the Brain tumor detector model¶
Below is an example from the grad-CAM visualisation applied to the brain tumor image classifier model.
In the grid below we see that the attention map is corresponding well with the clinicians mask - eventhough the classifier has never seen or beentrained on the mask.
This block loops over a set of test images (here: cases predicted as containing tumor), loading each MRI slice and its ground-truth mask (if available), and then computing a Grad-CAM attention map for the classifier’s decision. The Grad-CAM attention map is contrast-enhanced for visibility. I chose to look further “in” than the very last convolutional layer (via the robust layer selection) because the final conv features can become too coarse or gradient-saturated, producing weak or even blank maps; earlier convolutional layers often preserve more spatial detail and yield attention maps that align better with the actual tumor region, making the explanation more informative and easier to interpret.
[1/103] skipped input_layer: Attempt to convert a value (None) with an unsupported type (<class 'NoneType'>) to a Tensor. [2/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv1_conv.png [3/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv1_relu.png [4/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv2_block1_1_conv.png [5/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv2_block1_1_relu.png [6/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv2_block1_2_conv.png [7/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv2_block1_2_relu.png [8/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv2_block1_0_conv.png [9/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv2_block1_3_conv.png [10/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv2_block1_out.png [11/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv2_block2_1_conv.png [12/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv2_block2_1_relu.png [13/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv2_block2_2_conv.png [14/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv2_block2_2_relu.png [15/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv2_block2_3_conv.png [16/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv2_block2_out.png [17/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv2_block3_1_conv.png [18/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv2_block3_1_relu.png [19/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv2_block3_2_conv.png [20/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv2_block3_2_relu.png [21/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv2_block3_3_conv.png [22/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv2_block3_out.png [23/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv3_block1_1_conv.png [24/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv3_block1_1_relu.png [25/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv3_block1_2_conv.png [26/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv3_block1_2_relu.png [27/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv3_block1_0_conv.png [28/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv3_block1_3_conv.png [29/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv3_block1_out.png [30/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv3_block2_1_conv.png [31/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv3_block2_1_relu.png [32/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv3_block2_2_conv.png [33/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv3_block2_2_relu.png [34/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv3_block2_3_conv.png [35/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv3_block2_out.png [36/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv3_block3_1_conv.png [37/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv3_block3_1_relu.png [38/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv3_block3_2_conv.png [39/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv3_block3_2_relu.png [40/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv3_block3_3_conv.png [41/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv3_block3_out.png [42/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv3_block4_1_conv.png [43/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv3_block4_1_relu.png [44/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv3_block4_2_conv.png [45/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv3_block4_2_relu.png [46/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv3_block4_3_conv.png [47/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv3_block4_out.png [48/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv4_block1_1_conv.png [49/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv4_block1_1_relu.png [50/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv4_block1_2_conv.png [51/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv4_block1_2_relu.png [52/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv4_block1_0_conv.png [53/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv4_block1_3_conv.png [54/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv4_block1_out.png [55/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv4_block2_1_conv.png [56/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv4_block2_1_relu.png [57/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv4_block2_2_conv.png [58/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv4_block2_2_relu.png [59/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv4_block2_3_conv.png [60/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv4_block2_out.png [61/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv4_block3_1_conv.png [62/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv4_block3_1_relu.png [63/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv4_block3_2_conv.png [64/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv4_block3_2_relu.png [65/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv4_block3_3_conv.png [66/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv4_block3_out.png [67/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv4_block4_1_conv.png [68/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv4_block4_1_relu.png [69/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv4_block4_2_conv.png [70/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv4_block4_2_relu.png [71/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv4_block4_3_conv.png [72/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv4_block4_out.png [73/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv4_block5_1_conv.png [74/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv4_block5_1_relu.png [75/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv4_block5_2_conv.png [76/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv4_block5_2_relu.png [77/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv4_block5_3_conv.png [78/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv4_block5_out.png [79/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv4_block6_1_conv.png [80/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv4_block6_1_relu.png [81/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv4_block6_2_conv.png [82/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv4_block6_2_relu.png [83/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv4_block6_3_conv.png [84/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv4_block6_out.png [85/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv5_block1_1_conv.png [86/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv5_block1_1_relu.png [87/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv5_block1_2_conv.png [88/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv5_block1_2_relu.png [89/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv5_block1_0_conv.png [90/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv5_block1_3_conv.png [91/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv5_block1_out.png [92/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv5_block2_1_conv.png [93/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv5_block2_1_relu.png [94/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv5_block2_2_conv.png [95/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv5_block2_2_relu.png [96/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv5_block2_3_conv.png [97/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv5_block2_out.png [98/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv5_block3_1_conv.png [99/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv5_block3_1_relu.png [100/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv5_block3_2_conv.png [101/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv5_block3_2_relu.png [102/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv5_block3_3_conv.png [103/103] saved -> /content/drive/MyDrive/Colab Notebooks/Explainable-AI/docs/pics/gradcam_by_layer/conv5_block3_out.png
Output hidden; open in https://colab.research.google.com to view.
Output hidden; open in https://colab.research.google.com to view.